package org.example.basic_sort;


import java.util.Arrays;

/**
 * 选择排序
 * 时间复杂度：O(n^2)
 * 空间复杂度：O(1)
 * 稳定性：不稳定
 */
public class SelectionSort {

    public static void selectionSort(int[] arr) {
        if(arr == null || arr.length <= 1) {
            return;
        }
        for (int i = 0; i < arr.length - 1 ; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length ; j++) {
                //minIndex = arr[j] < arr[minIndex]? j : minIndex;
                if(arr[i] > arr[j]) {
                    swap(arr, i, j);
                }

            }

        }
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        int[] arr = {1,5,3,6,8};
        selectionSort(arr);
        for (int i : arr) {
            System.out.println(i);
        }
    }
}
